package com.example.spinning.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spinning.bean.PaginationResult;
import com.example.spinning.bean.Spooling;
import com.example.spinning.mapper.SpoolingMapper;
import com.example.spinning.utils.CraftManagementTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author bayernxmt
 * @create 2020-07-28-16:43
 */
@Service
@Transactional
public class SpoolingService {
    @Autowired
    private SpoolingMapper spoolingMapper;

    //查询所有 并且分页展示 : current表示当前页，size表示一页展示多少条数据
    public PaginationResult<Spooling> selectAll(int current, int size){
        Page<Spooling> pages = new Page<>(current,size);
        spoolingMapper.selectPage(pages, null);
        List<Spooling> list = pages.getRecords();
        int total = spoolingMapper.selectCount(null);

        PaginationResult<Spooling> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //单、多条件查询 并且实现分页
    public PaginationResult<Spooling> selectByMulParams(int current, int size, Spooling spooling, String dateTime){

        QueryWrapper<Spooling> wrapper = new QueryWrapper();
        if (null!=spooling.getBatch_no())
            wrapper.eq("batch_no", spooling.getBatch_no());
        if (null!=spooling.getName())
            wrapper.eq("name", spooling.getName());
        if (null!=dateTime)
            wrapper.like("date", dateTime);
        if (null!=spooling.getWorkshop())
            wrapper.eq("workshop", spooling.getWorkshop());
        if (null!=spooling.getSpun_yarn_num())
            wrapper.eq("workshop", spooling.getSpun_yarn_num());

        Page<Spooling> pages = new Page<>(current,size);
        spoolingMapper.selectPage(pages, wrapper);
        int total = spoolingMapper.selectCount(wrapper);
        List<Spooling> list = pages.getRecords();

        PaginationResult<Spooling> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //根据id查询
    public Spooling SelectById(int id){
        return spoolingMapper.selectById(id);
    }

    //插入数据
    public int insert(Spooling spooling) {
        spooling.setDate(CraftManagementTimeUtils.getTimestamp());
        int result = spoolingMapper.insert(spooling); //result为受到影响的条数
        return result;
    }


    //批量删除数据（与删除一条合并）
    public int delete(List<Integer> ids){
        int result = spoolingMapper.deleteBatchIds(ids);
        return result;
    }

    //修改数据(已经实现了动态sql)
    public int update(Spooling spooling){
        spooling.setDate(new Timestamp(new Date().getTime()));
        int result = spoolingMapper.updateById(spooling);
        return result;
    }
}
